from selenium import webdriver
import time



# browser = webdriver.Chrome()

url = "https://www.baidu.com/"
# url = "https://www.12306.cn/index/index.html"
# url = "https://www.zhihu.com/signin?next=%2F"
# url = "https://accounts.douban.com/passport/login_popup?login_source=anony"
# browser.get(url)
# browser.implicitly_wait(10)

# 知乎
# browser.find_element_by_name("username").send_keys('15102115904')
# browser.find_element_by_name("password").send_keys('199213xiao..')
# browser.find_element_by_xpath("//button[@type='submit']").click()


# 豆瓣点击checkbox
# browser.find_element_by_id("account-form-remember").click()


# select
# from selenium.webdriver.support.ui import Select
# from selenium.webdriver.common.by import By
# select = Select(browser.find_element(By.ID,''))
# select.select_by_index(1)     # 索引
# select.select_by_value('')  # value值
# select.select_by_visible_text('')   # 可见的文字内容



# 鼠标操作
# from selenium.webdriver.common.action_chains import ActionChains
# # 鼠标操作
# action = ActionChains(browser)
# username = browser.find_element_by_name('username')
# password = browser.find_element_by_name('password')
# submit = browser.find_element_by_xpath("//button[@type='submit']")
# action.move_to_element(username)
# action.send_keys_to_element(username,'15102115904')
# action.move_to_element(password)
# action.send_keys_to_element(password,'199213xiao..')
# action.move_to_element(submit)
# action.click()
# # 执行
# action.perform()





# cookie操作
# 获取所有cookie
# cookies = browser.get_cookies()
# for i in cookies:
#     print(i)
# 根据cookie的key获取value
# domain = browser.get_cookie('domain')
# print(domain)
# 删除所有的cookie
# browser.delete_all_cookies()
# 删除某个cookie
# browser.delete_cookie('domain')
# 添加cookie
# browser.add_cookie({'name':'username','value':'123'})
# cookies = browser.get_cookies()
# for i in cookies:
#     print(i)
# cookie = browser.get_cookie('username')
# print(cookie)





# 页面等待
# 隐试等待：implicitly_wait  设置时间 时间过后找不到就报错


# 显示等待
# from selenium.webdriver.support import expected_conditions  as EC
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.common.by import By
#
# WebDriverWait(browser,100).until(EC.text_to_be_present_in_element_value((By.ID,'fromStationText'),'长沙'))
# WebDriverWait(browser,100).until(EC.text_to_be_present_in_element_value((By.ID,'toStationText'),'北京'))
# browser.find_element_by_id('search_one').click()



# 窗口切换
# browser.execute_script('window.open("https://www.douban.com/")')
# browser.switch_to.window(browser.window_handles[1])  # 获取第二个窗口 0开始
# print(browser.page_source)



# selenium设置代理
option = webdriver.ChromeOptions()
option.add_argument('--proxy-server=http://183.154.214.186:9000')
browser = webdriver.Chrome(chrome_options=option)
browser.get('http://httpbin.org/ip')

# browser.save_screenshot('1.png') # 截图
# div = browser.find_element_by_id('')
# div.get_attribute() # 官方的和非官方的属性值都可以获取
# div.get_property()  # 只能获取官方的属性值


# time.sleep(5)
# browser.close()  # 关闭当前页面
# browser.quit()   # 退出浏览器